<?php
/**
 * Created by PhpStorm.
 * User: ZhangWei
 * Date: 2018/3/16
 * Time: 14:57
 */

namespace App\Http\Controllers\Api\Open;


use App\Http\Resources\MoneyLogCollection;
use App\Http\Resources\UserBuyOrderCollection;
use App\Http\Resources\WithdrawCollection;
use App\Models\MoneyLog;
use App\Models\UserBuyOrder;
use App\Models\Withdraw;

class PayController extends BaseController
{
    /**
     * @return mixed
     * @throws \App\Exceptions\ApiException
     */
    public function invoice()
    {
        $user = $this->getUser();
        $order = UserBuyOrder::query()->where('l_user_id', $user->id)->where('pay_state', 1)->orderByDesc('pay_time');

        $date = request('date', false);
        if ($date) {
            $b_arr = explode(',', $date);
            $order->whereBetween('pay_time', $b_arr);
        }


        $order->with(['user', 'liveroom_log']);
        $page = $order->paginate(request('size', 15));

        $data = UserBuyOrderCollection::make($page);

        return $this->success($data);
    }

    public function transaction()
    {
        $user = $this->getUser();
        $money_log = MoneyLog::query()->where('user_id', $user->id)->orderByDesc('id');
        $date = request('date', false);
        if ($date) {
            $b_arr = explode(',', $date);
            if (is_array($b_arr) && count($b_arr) == 2) {
                $money_log->whereBetween('created_at', $b_arr);
            }
        }

        $page = $money_log->paginate(request('size', 15));

        $data = MoneyLogCollection::make($page);
        return $this->success($data);
    }

    public function withdraw_record()
    {
        $user = $this->getUser();
        $withdraw_log = Withdraw::query()->where('user_id', $user->id)->orderByDesc('id');
        $date = request('date', false);
        if ($date) {
            $b_arr = explode(',', $date);
            if (is_array($b_arr) && count($b_arr) == 2) {
                $withdraw_log->whereBetween('created_at', $b_arr);
            }
        }

        $page = $withdraw_log->paginate(request('size', 15));

        $data = WithdrawCollection::make($page);
        return $this->success($data);
    }

}